<!-- 正文开始 -->
<style>
    .w-120 {
        width: 115px;
    }
    xm-select > .xm-body .xm-search > i{
        right: 10px;
    }
    .w-500 {
        width: 500px !important;
    }

    .title-txt {
        padding: 0 0 20px 30px;
        font-weight: 600;
        font-size: 18px;
    }

    .conentStyle {
        padding: 10px;
        box-sizing: border-box;
    }

    .conentBg {
        padding: 20px;
        box-sizing: border-box;
        border-radius: 5px;
        background-color: #fff;
        min-height: 660px;
    }

    .borderNone {
        border: none;
    }

    .formBg {
        background-color: #F7F8FA;
        padding: 20px;
    }

    .layui-table-cell {
        height: auto;
    }

    .layui-table-cell img {
        width: 80px;
        height: 80px;
    }

    .import {
        opacity: 0;
        position: absolute;
        width: 69px;
        height: 38px;
        margin-left: -10px;
    }
    .addMaterialSkuForm .layui-form-label{
        width: 90px;
    }
    .addMaterialSkuForm .layui-input-block{
        margin-left: 120px;
    }
    .productForm .layui-form-select{
        width: 100%;
    }
    .productForm .xm-label .label-content{
        color: #333;
    }
    .material-body {
        display: flex;
        height: 100% !important;
        padding: 0 15px 0 0 !important;
    }

    .material-nav {
        width: 102px;
        background: rgba(0, 0, 0, .01);
    }

    .material-nav .nav-cell {
        padding: 10px 5px 10px 10px;
        cursor: pointer;
        position: relative;
        display: flex;
        align-items: center;
    }

    .material-nav .nav-cell:hover {
        color: #333;
        transition: all .3s;
    }

    .material-nav .nav-cell.nav-active {
        color: #333;
    }

    .material-nav .nav-cell.nav-active::before {
        content: '';
        display: inline-block;
        height: 14px;
        width: 3px;
        background: #1BA70E;
        position: absolute;
        left: 0;
    }

    .material-body .body-left {
        width: 340px;
        height: 100%;
        overflow: hidden;
    }

    .body-left .layui-tab-content {
        height: 100%;
        padding: 0;
        flex: 1;
        border-right: 1px solid #e6e6e6;
    }

    .body-left .layui-tree {
        padding: 5px;
    }

    .body-left .layui-tab {
        height: 100%;
        color: #999;
        display: flex;
        overflow: auto;
    }

    .body-left .layui-tab-title li {
        padding: 0 10px;
    }

    .material-body .body-right {
        width: calc(100% - 355px);
        padding: 15px 0 15px 15px;
    }

    .layui-tree-emptyText {
        padding-top: 5px;
    }

    .body-right xm-select {
        min-height: 32px;
        line-height: 32px;
    }

</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body material-body">
            <!-- 头部操作栏 -->
            <div class="body-left">
                <div class="layui-tab layui-tab-brief">
                    <div class="material-nav" id="materialNav"></div>
                    <div class="layui-tab-content material-tree" id="productTypeTree"></div>
                </div>
            </div>
            <div class="body-right">
                <div class="layui-form lay-header">
                    <div class="toolbar">
                        <div class="layui-form-item lay-query-content">
                            <div class="lay-query-box">
                                <div class="query-lt">
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">物料名称：</label>
                                        <div class="layui-input-inline">
                                            <input name="name" class="layui-input" type="text"
                                                   placeholder="请输入货品名称"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">物料编码：</label>
                                        <div class="layui-input-inline">
                                            <input name="productCode" class="layui-input" type="text"
                                                   placeholder="请输入货品编码"/>
                                        </div>
                                    </div>
                                </div>
                                <div class="query-rt">
                                    <div class="layui-inline">
                                        <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                                lay-submit>查询
                                        </button>
                                        <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="lay-btn-rows">
                        <div class="lay-btn-box">
                            <div class="lay-btn-lt">
                                <button id="productBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                                <button id="fromLocalWarehouseBtnImport" class="layui-btn icon-btn color-reseda">
                                    <input id="uploaderInput" type="file" name="file" class="import" accept=".xls,.xlsx"/>导入
                                </button>
                            </div>
                            <div class="lay-btn-rt">
                                <button id="fromLocalWarehouseBtnTemplate" class="layui-btn icon-btn color-green">模板下载
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="productTable" lay-filter="productTable"></table>
            </div>
        </div>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="productTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 编辑物料表格操作列 -->
<script type="text/html" id="skuTableBar">
    {{# if(d.isShow==true){  }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="frozen">冻结</a>
    {{# }else{ }}
    <a class="layui-btn layui-btn-xs" lay-event="thaw">解冻</a>
    {{# } }}
</script>

<!-- 添加/修改表单弹窗 -->
<script type="text/html" id="productForm">
    <form lay-filter="productForm" class="layui-form productForm form-scroll model-form">
        <input name="id" type="hidden"/>
        <input name="productTypeId" type="hidden"/>
        <div class="open-details-box pd-20">
            <div class="custom-table" style="padding-top: 20px;">
                <ul class="t-header page-flex">
                    <li>选择类别</li>
                    <li>产品性质</li>
                    <li>计价方式</li>
                    <li>选择产品分类</li>
                    <li>生产工艺路线</li>
                </ul>
                <div class="t-body page-flex">
                    <div class="cell">
                        <div id="selectProductType" style="width: 100%"></div>
                    </div>
                    <div class="cell">
                        <select name="productNature" id="productNature" lay-filter="productNature" lay-verify="required"></select>
                    </div>
                    <div class="cell">
                        <select name="calculateWay" id="calculateWay" lay-filter="calculateWay" lay-verify="required">
                        </select>
                    </div>
                    <div class="cell">
                        <select name="productCategoryId" id="productCategoryId" lay-filter="productCategoryId" lay-verify="required">
                        </select>
                    </div>
                    <div class="cell">
                        <select name="productProcessLineId" id="productProcessLineId" lay-filter="productProcessLineId" lay-verify="required">
                        </select>
                    </div>
                </div>
            </div>
            <div class="custom-table" style="padding-top: 20px;">
                <ul class="t-header page-flex">
                    <li>物资编号</li>
                    <li>物资名</li>
                    <li>旧物资名</li>
                    <li>旧物资编号</li>
                </ul>
                <div class="t-body page-flex">
                    <div class="cell">
                        <input name="productCode" class="layui-input" type="text" placeholder="请输入" lay-verify="required"/>
                    </div>
                    <div class="cell">
                        <input name="name" class="layui-input" type="text" placeholder="请输入" lay-verify="required"/>
                    </div>
                    <div class="cell">
                        <input name="historyName" class="layui-input" type="text" placeholder="请输入" lay-verify="required"/>
                    </div>
                    <div class="cell">
                        <input name="historyProductCode" class="layui-input" type="text" placeholder="请输入" lay-verify="required"/>
                    </div>
                </div>
            </div>
            <div class="layui-form-item table-bor scroll-table-5">
                <div class="layui-form-item" style="margin: 20px 0">
                    <div class="open-details-title">产品SKU信息</div>
                </div>
                <button class="layui-btn addTable addMaterialSku" type="button">添加SKU</button>
                <table class="layui-table" id="materialSkuTable" lay-filter="materialSkuTable"></table>
            </div>
        </div>
        <div class="layui-form-item text-right pd-20">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="productFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script type="text/html" id="materialNavTemplate">
    {{# layui.each(d, function(index, item){ }}
    <div class="nav-cell {{# if(item.isShow){ }}nav-active{{# } }}" data-value="{{item.labelValue}}"><span>{{item.labelName}}</span>
    </div>
    {{# }); }}
</script>

<!--产品SKU信息表格操作按钮-->
<script type="text/html" id="materialSkuTableBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">移除</a>
</script>

<!--添加产品sku信息弹窗-->
<script type="text/html" id="addMaterialSkuForm">
    <form lay-filter="addMaterialSkuForm" class="layui-form form-scroll model-form">
        <input name="index" type="hidden"/>
        <input name="baseUnitName" type="hidden"/>
        <input name="showStandardName" type="hidden"/>
        <div class="open-details-box pd-20 addMaterialSkuForm">
            <div class="layui-form-item" style="padding-top: 20px;min-height: 50px;margin-bottom: 10px;">
                <label class="layui-form-label">商品图片</label>
                <div id="btnUpload14" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传图片
                </div>
                <div id="btnUpload14_path" style="margin-left: 260px;line-height: 40px;"></div>
                <input id="productPic" name="productPic" type="hidden"/>
            </div>
            <div class="layui-form-item" style="display: flex;">
                <label class="layui-form-label w-auto" style="flex-shrink: 0;padding-left: 7px;">标准规格码参数</label>
                <div id="standardSpec"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">规格码</label>
                <div class="layui-input-block">
                    <input name="skuSpec" class="layui-input" type="text" placeholder="请输入规格码" lay-verify="required"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">旧规格码</label>
                <div class="layui-input-block">
                    <input name="oldSpecCode" class="layui-input" type="text" placeholder="请输入旧规格码"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">基础单位</label>
                <div class="layui-input-block">
                    <select name="baseUnit" id="baseUnit" lay-filter="baseUnit" lay-verify="required">
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">基础单位数</label>
                <div class="layui-input-block">
                    <input name="baseUnitRate" class="layui-input" type="text" placeholder="请输入基础单位数" lay-verify="required"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">SKU编号</label>
                <div class="layui-input-block">
                    <input name="skuCode" class="layui-input" type="text" placeholder="请输入SKU编号" lay-verify="required"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">SKU产品名称</label>
                <div class="layui-input-block">
                    <input name="skuName" class="layui-input" type="text" placeholder="请输入SKU产品名称" lay-verify="required"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">货品条码</label>
                <div class="layui-input-block">
                    <input name="standardBarCode" class="layui-input" type="text" placeholder="请输入货品条码"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right pd-20">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="addMaterialSkuFormSubmit" lay-submit>确定</button>
        </div>
    </form>
</script>

<!--标准规格码参数模板-->
<script type="text/html" id="standardSpecTemplate">
    {{# layui.each(d, function(index, item){ }}
        {{# if(item.style=='text'){ }}
            <div class="layui-input-inline" style="margin-bottom: 4px;float: inherit;">
                <input name="inputVal{{index}}" class="layui-input" type="text" placeholder="请输入"/>
            </div>
        {{# }else{ }}
            <div class="layui-input-inline" style="margin-bottom: 5px;float: inherit;">
                <select name="selectVal{{index}}">
                    <option value="">请选择</option>
                    {{# layui.each(item.detailList, function(idx, ele){ }}
                        {{#
                            let showText = item.specLocation == 'before' ? item.specHead + ele : ele + item.specHead
                        }}
                        <option value="{{showText}}">{{showText}}</option>
                    {{# }); }}
                </select>
            </div>
        {{# } }}
    {{# }); }}
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin',
        'layedit', 'tableTreeDj', 'newTree', 'laydate', 'upload', 'tree', 'laytpl'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var layedit = layui.layedit;
        var upload = layui.upload;
        var tableTree = layui.tableTreeDj;
        var newTree = layui.newTree;
        var laydate = layui.laydate;
        var upload = layui.upload;
        var laytpl = layui.laytpl;

        // 获取类别性质
        admin.req('enterprise/dict/admin/dictLabel/list', {
            dictType: 'sys_product_type_nature',
            requestType: 'research'
        }, res => {
            if (res.code == 200 && res.data.length > 0) {
                res.data.forEach(item => {
                    item.isShow = false
                })
                res.data[0].isShow = true;
                getProductType(res.data[0].labelValue)
                laytpl(materialNavTemplate.innerHTML).render(res.data, function (html) {
                    $('#materialNav').html(html);
                    $('#materialNav .nav-cell').click(function () {
                        let val = $(this).data('value');
                        $(this).addClass("nav-active").siblings().removeClass("nav-active");
                        getProductType(val)
                    })
                });
            }
        });

        //物料类型
        function getProductType(materialNature) {
            admin.req('enterprise/product/admin/productType/list', {
                enterprise: 0,
                limit: 99999,
                materialNature: materialNature
            }, res => {
                if (res.code == 200) {
                    let treeList = res.data.filter(item => {
                        return item.isDeleted
                    })
                    initTree(treeList)
                    // loadSelectHtml(res.data, '请选择物料类型', 'productTypeId', 'name');
                } else {
                    // loadSelectHtml([], '请选择物料类型', 'productTypeId', 'name')
                }
            });
        }

        function initTree(data) {
            layui.tree.render({
                elem: '#productTypeTree' //传入元素选择器
                , data: formatToTree(data, "-1"),
                click: function (obj) {
                    insTb.reload({where: {productTypeId: obj.data.id}, page: {curr: 1}}, 'data');
                }
            });
        }

        function formatToTree(ary, pid, pidName = 'parentId') {
            return ary
                .filter((item) => item[pidName] === pid)
                .map((item) => {
                    // 通过父节点ID递归查询所有子节点
                    item.children = formatToTree(ary, item.id);
                    item.label = item.name;
                    // item.spread = true
                    return item;
                });
        }

        // 重置搜索
        form.on('submit(resets)', function (data) {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        // 渲染表格
        var insTb = table.render({
            elem: '#productTable',
            url: config.base + 'enterprise/product/admin/product/list',
            page: true,
            toolbar: true,
            defaultToolbar: ["filter"],
            where: {
                "dataSourceType": 0,
                "type": 0
            },
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', fixed: 'left'}
                , {
                    field: 'picture',
                    title: '商品图片',
                    event: 'imgClick',
                    fixed: 'left',
                    align: 'center',
                    width: 110,
                    templet: function (d) {
                        if (d.picture) {
                            return '<img src="' + d.picture + '" style="cursor: pointer"/>';
                        }
                        return '<img src="assets/images/default-icon.png" style="cursor: pointer"/>';
                    }
                }
                , {field: 'name', align: 'center', title: '货品名称', width: 200}
                , {field: 'productCode', align: 'center', title: '货品编码', width: 200}
                , {field: 'materialNatureName', align: 'center', title: '物料类别性质', width: 180}
                , {field: 'productTypeName', align: 'center', title: '所属类别'}
                , {field: 'productCategoryName', align: 'center', title: '物料分类'}
                , {field: 'baseUnitName', align: 'center', title: '基础单位'}
                , {field: 'createTime', align: 'center', title: '创建时间', width: 180}
                , {align: 'center', toolbar: '#productTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
            }
        });

        // 工具条点击事件
        table.on('tool(productTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(productTable)', function (obj) {
            table.reload('productTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        //渲染下拉框
        form.render('select');

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            data.field.dataSourceType = 0;
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: ['1368px', '820px'],
                offset: '65px',
                title: data ? '修改物料' : '添加物料',
                content: $('#productForm').html(),
                success: function () {
                    var productDetailList = data ? data.productDetailList : [];
                    //获取产品类别
                    admin.initDataSelectTree(true, 'enterprise/product/admin/productType/getTreeData', {limit: 99999}, "selectProductType", data ? data.productTypeId : null, "产品类别", "productTypeId", function (row) {
                        $("input[name='productTypeId']").val(row.arr[0].value)
                    });
                    // 获取产品性质
                    admin.getDictList("productNature", "product_nature", data.productDetailList[0] ? data.productDetailList[0].productNature : null, "产品性质");
                    // 获取计价方式
                    admin.getDictList("calculateWay", "sys_product_detail_calculate_way", data ? data.calculateWay : null, "库存计量方式");
                    // 获取产品分类
                    admin.initDataSelect("enterprise/product/admin/productCategory/list", {limit: 9999}, "productCategoryId", data ? data.productCategoryId : null, "物料分类", null, null);
                    // 获取工艺路线
                    admin.initDataSelect("enterprise/product/admin/processLineTemplate/list", {limit: 99999}, "productProcessLineId", data ? data.productProcessLineId : null, "工艺线路", null, null);

                    var materialSkuTable = table.render({
                        elem: '#materialSkuTable',
                        data: productDetailList,
                        cellMinWidth: 120,
                        cols: [[
                            {type: 'numbers', title: '序号', fixed: 'left'}
                            , {
                                field: 'productPic',
                                title: 'SKU图片',
                                event: 'imgClick',
                                fixed: 'left',
                                align: 'center',
                                width: 110,
                                templet: function (d) {
                                    if (d.productPic) {
                                        return '<img src="' + d.productPic + '" style="cursor: pointer"/>';
                                    }
                                    return '<img src="assets/images/default-icon.png" style="cursor: pointer"/>';
                                }
                            }
                            , {field: 'skuSpec', title: '规格码', align: 'center'}
                            , {field: 'oldSpecCode', title: '旧规格码', align: 'center'}
                            , {field: 'baseUnitName', title: '基础单位', align: 'center'}
                            , {field: 'baseUnitRate', title: '基础单位数', align: 'center'}
                            , {field: 'skuCode', title: 'SKU编号', align: 'center'}
                            , {field: 'skuName', title: 'SKU产品名称', align: 'center'}
                            , {field: 'standardBarCode', title: '货品条码', align: 'center'}
                            , {align: 'center', toolbar: '#materialSkuTableBar', title: '操作', width: 140}
                        ]],
                    });

                    // 工具条点击事件
                    table.on('tool(materialSkuTable)', function (obj) {
                        let data = obj.data;
                        if (obj.event === 'edit') { //修改
                            addMaterialSkuFormTemplate(data);
                        } else if (obj.event === 'del') { // 删除
                            productDetailList.splice(data.index, 1)
                            table.reload('materialSkuTable', {
                                data: productDetailList,
                            });
                        }
                    });

                    // 添加SKU
                    $(".addMaterialSku").click(function () {
                        if ($("input[name='productTypeId']").val() == '') {
                            layer.msg('请选择类别', {icon: 2});
                            return false;
                        }
                        addMaterialSkuFormTemplate();
                    });

                    // 添加SKU信息
                    function addMaterialSkuFormTemplate(skuData) {
                        var addMaterialSkuFormOpen = admin.open({
                            type: 1,
                            title: '产品SKU信息',
                            area: ['600px', '700px'],
                            offset: '100px',
                            content: $('#addMaterialSkuForm').html(),
                            success: function () {
                                var standardSpecData = [];
                                admin.getDictList("baseUnit", "sys_product_detail_base_unit", skuData ? skuData.baseUnit : null, "基础单位");
                                // admin.initDataSelect("enterprise/product/admin/productUnit/list", {
                                //     productType: $("input[name='productTypeId']").val(),
                                //     limit: 9999,
                                // }, "baseUnit", skuData ? skuData.baseUnit : null, "基础单位", null, null);
                                //查询企业信息
                                var enterpriseInfo;
                                admin.req('enterprise/system/admin/enterpricceId/getEnterpricceIdVO', {}, function (res) {
                                    enterpriseInfo = res.data;
                                }, 'get', false);
                                if("" == enterpriseInfo || null == enterpriseInfo){
                                    layer.msg("企业信息获取异常", {icon: 1});
                                    return;
                                }
                                upload.render({
                                    elem: '#btnUpload14'
                                    , url: config.upload_url + 'uploadFile'
                                    , data: {"path": enterpriseInfo.enterpriseCode + "/"}
                                    , accept: 'file'
                                    , exts: 'jpg|png'
                                    , headers: {Authorization: 'Bearer ' + config.getToken()}
                                    , done: function (res) {
                                        if (res.code == 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            var imagUrl = res.data.url;
                                            $('#btnUpload14_path').html('<img src="' + imagUrl + '" style="height: 50px;width: 50px" />');
                                            $('#productPic').val(imagUrl);
                                        } else if (res.code == 502) {
                                            return location.replace('admin/login.html');
                                        } else {
                                            layer.msg(res.msg, {icon: 2});
                                        }
                                    }
                                });

                                if (skuData && skuData.picture) {
                                    $('#btnUpload14_path').html('<img src="' + data.picture + '" style="height: 50px;width: 50px;" />');
                                }

                                // 下拉框文本
                                form.on('select(baseUnit)', function (obj) {
                                    var selectedText = $("#baseUnit option:selected").text();
                                    $("input[name='baseUnitName']").val(selectedText)
                                })

                                // 获取标准规格参数
                                admin.req('enterprise/product/admin/productSpec/list', {
                                    productType: $("input[name='productTypeId']").val(),
                                    limit: 9999,
                                }, function (res) {
                                    if (res.code == 200) {
                                        standardSpecData = res.data;
                                        standardSpecData.forEach(item => {
                                            if (item.style == 'single') {
                                                item.detailList = item.specValues.split(',')
                                            }
                                        })
                                        laytpl(standardSpecTemplate.innerHTML).render(standardSpecData, function (html) {
                                            $('#standardSpec').html(html);
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }, 'GET', false);

                                form.val('addMaterialSkuForm', skuData);
                                form.render("select");

                                $('#standardSpec').on('input', 'input', function() {
                                    $("input[name='skuSpec']").val(getStandardName())
                                });
                                form.on('select', function(){
                                    $("input[name='skuSpec']").val(getStandardName())
                                })

                                // 标准规格码参数
                                function getStandardName() {
                                    let showStandardNameList = [];
                                    standardSpecData.forEach((item, index) => {
                                        if (item.style == 'single') {
                                            let selectVal = 'selectVal' + index;
                                            let txt = $(`select[name=${selectVal}]`).val()
                                            if (txt != '') {
                                                showStandardNameList.push(txt)
                                            }
                                        } else {
                                            let inputVal = 'inputVal' + index;
                                            let val = $(`input[name=${inputVal}]`).val()
                                            if (val != '' && item.specCode == 'is') {
                                                let showText = item.specLocation == 'before' ? item.specHead + val : val + item.specHead;
                                                showStandardNameList.push(showText)
                                            }
                                        }
                                    });
                                    return showStandardNameList.length > 0 ? showStandardNameList.join(',') : '';
                                }
                                // 确认
                                form.on('submit(addMaterialSkuFormSubmit)', function (d) {
                                    let paramsData = d.field;
                                    if (skuData) {
                                        if (data) {
                                            // 如果是修改物料
                                            productDetailList.forEach(item => {
                                                if (item.id == skuData.id) {
                                                    for (let key in item) {
                                                        if (key in paramsData) {
                                                            item[key] = paramsData[key]
                                                        }
                                                    }
                                                    item.baseUnitName = $("#baseUnit option:selected").text()
                                                }
                                            })
                                        } else {
                                            productDetailList[paramsData.index] = paramsData
                                        }
                                    } else {
                                        paramsData.index = productDetailList.length
                                        productDetailList.push(paramsData)
                                    }
                                    // 渲染表格
                                    table.reload('materialSkuTable', {
                                        data: productDetailList,
                                    });
                                    layer.close(addMaterialSkuFormOpen);
                                    return false;
                                })
                            }
                        })
                    }

                    form.val('productForm', data);
                    // 表单提交事件
                    form.on('submit(productFormSubmit)', function (d) {
                        if (productDetailList.length == 0) {
                            layer.msg('请添加SKU信息', {icon: 2});
                            return false;
                        }
                        productDetailList.forEach(item => {
                            item.productNature = $("select[name='productNature']").val()
                        })
                        let params = {
                            ...d.field,
                            productDetailList
                        }
                        admin.req(data ? 'enterprise/product/admin/product/update' : 'enterprise/product/admin/product/add', JSON.stringify(params), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('productTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST', true, 'application/json; charset=utf-8');
                        return false;
                    });
                }

            });
        }

        // 添加按钮点击事件
        $('#productBtnAdd').click(function () {
            showEditModel();
        });

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/product/admin/product/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#productBtnExport').click(function () {
            var checkRows = table.checkStatus('productTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

        //导入excel
        $('#uploaderInput').on("change", function (e) {
            layer.load(2);
            var formData = new FormData();
            var files = e.target.files;
            let that = $(this);
            formData.append("file", files[0]);
            formData.append("name", files[0].name);//这个地方可以传递多个参数
            $.ajax({
                url: config.base + 'enterprise/product/admin/product/importExcel',
                data: formData,
                headers: {Authorization: 'Bearer ' + config.getToken()},
                type: "POST",
                processData: false,
                contentType: false,
                success: function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        // table.reload('humanResourcesTable');
                        layer.closeAll('page');
                        setTimeout(_ => {
                            location.reload()
                        }, 1000);
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                    that.val('');
                }
            });
        })

        // 模板下载
        $('#fromLocalWarehouseBtnTemplate').click(function () {
            window.location.href = config.base + "enterprise/product/admin/product/template";
        });

    });

</script>
